﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using SMTC.Model.Domain;
using System.Data.Common;
using IMPlant.Core.Data.Unity;
using System.Data;
using SMTC.Model.Model;

namespace SMTC.Dao.HolidayMangement
{
    public class HolidayProccessDao : BaseDao
    {
        /// <summary>
        /// 插入假期
        /// </summary>
        private const string SQL_INSERT_HOLIDAY = @"INSERT INTO dbo.SMT_Holiday (
	                                                    HolidayDate,
	                                                    Discrib,
	                                                    ModifyPerson,
	                                                    ModifyTime
                                                    ) VALUES ( 
	                                                    /* HolidayDate - datetime */ @HolidayDate,
	                                                    /* Discrib - nvarchar(50) */ @Discrib,
	                                                    /* ModifyPerson - nchar(10) */ @ModifyPerson,
	                                                    /* ModifyTime - datetime */ GETDATE() )";

        private const string SQL_MODIFY_HOLIDAY = @"UPDATE dbo.SMT_Holiday
                                                    SET 
                                                    Discrib = @Discrib
                                                    ,ModifyPerson = @ModifyPerson
                                                    ,ModifyTime = GETDATE()
                                                    WHERE DATEDIFF(DAY,HolidayDate,@HolidayDate) = 0";
        /// <summary>
        /// 删除休假信息
        /// </summary>
        private const string SQL_DELETE_HOLIDAY = @"DELETE FROM dbo.SMT_Holiday
                                                    WHERE DATEDIFF(DAY,HolidayDate,@HolidayDate) = 0";
        /// <summary>
        /// 有效工作日减1
        /// </summary>
        private const string SQL_UPDATE_WORKDAYS_SUB = @"UPDATE dbo.SMT_Month
                                                        SET WorkDays = WorkDays -1
                                                        WHERE [Month] = @Month";
        /// <summary>
        /// 有效工作日加1
        /// </summary>
        private const string SQL_UPDATE_WORKDAYS_ADD = @"UPDATE dbo.SMT_Month
                                                        SET WorkDays = WorkDays +1
                                                        WHERE [Month] = @Month";

        public bool InsertHoliday(SMT_HolidayModel holiday)
        {
            DbParameter[] parameters = 
            {
                DBparameterBuild.Build("@HolidayDate",holiday.HolidayDate)
               ,DBparameterBuild.Build("@Discrib",holiday.Discrib)
               ,DBparameterBuild.Build("@ModifyPerson",holiday.ModifyPerson)
            };

            this.ComputWorkDays(holiday.HolidayDate);
            return DBTemplete.ExcuteNoQuery(CommandType.Text, SQL_INSERT_HOLIDAY, parameters) > 0;
        }

        public bool UpdateHoliday(SMT_HolidayModel holiday)
        {
            DbParameter[] parameters = 
            {
                DBparameterBuild.Build("@HolidayDate",holiday.HolidayDate)
               ,DBparameterBuild.Build("@Discrib",holiday.Discrib)
               ,DBparameterBuild.Build("@ModifyPerson",holiday.ModifyPerson)
            };

            return DBTemplete.ExcuteNoQuery(CommandType.Text, SQL_MODIFY_HOLIDAY, parameters) > 0;
        }

        public bool DeleteHoliday(DateTime dt)
        {
            DbParameter[] parameters =
            {
                DBparameterBuild.Build("@HolidayDate",dt)
            };
            this.ComputWorkDays(dt,false);
            return DBTemplete.ExcuteNoQuery(CommandType.Text, SQL_DELETE_HOLIDAY, parameters) > 0;
        }

        public bool ComputWorkDays(DateTime dt,bool isaddHoliday = true)
        {
            string SQl = "";
            if (isaddHoliday == true)
            {
                SQl = SQL_UPDATE_WORKDAYS_SUB;
            }
            else
            {
                SQl = SQL_UPDATE_WORKDAYS_ADD;
            }

            DbParameter[] parameters = { DBparameterBuild.Build("@Month", dt.Month) };
            return DBTemplete.ExcuteNoQuery(CommandType.Text, SQl, parameters) > 0;
        }
    }
}
